/**
 * GenerateNames
 * 
 * 26/10/2013
 */
package com.jtmproject.struts.tulebox.classes;

import java.util.Random;

/**
 * this class generates the name based on the prefix passed by parameters
 * @author Javier Tejedor
 */
public class GenerateNames {

	private final static String ELVISH = "elvish";
	private final static String ORCHIS = "orchis";
	
	private final static String[] ENDING_ELVISH = 
		{"il", "al", "el", "in", "ir", "er"}; 
	
	private final static String[] ENDING_ORCHIS = 
		{"ur", "og", "ag", "un", "ar", "ut"};
	
	/**
	 * this function do the action to generate the names
	 * @param prefixSelected
	 * @param numberNamesToGenerated
	 * @return
	 */
	public String generateNames(int prefixSelected, int numberNamesToGenerated){
		String prefix = getPrefix(prefixSelected);
		if(prefix != null){
			return generate(prefix, numberNamesToGenerated);
		}
		
		return "";
	}
	
	/**
	 * this function generates the names
	 * @param prefix
	 * @param numberNamesToGenerated
	 * @return
	 */
	private String generate(String prefix, int numberNamesToGenerated){
		
		String namesGenerated = "";
		String[] sufix = numberOfSufix(prefix);
		
		for(int i = 0; i < numberNamesToGenerated; i++){
			namesGenerated += prefix + sufix[new Random().nextInt(sufix.length)] + " ";
		}
		
		return namesGenerated;
	}
	
	/**
	 * this function returns the array belong to the prefix
	 * @param prefix
	 * @return
	 */
	private String[] numberOfSufix(String prefix){
		
		if(prefix.equals(ELVISH)){
			return ENDING_ELVISH;
		}else if(prefix.endsWith(ORCHIS)){
			return ENDING_ORCHIS;
		}
		
		return null;
	}
	
	/**
	 * this function returns a String of prefix selected
	 * @param prefixSelected
	 * @return
	 */
	private String getPrefix(int prefixSelected){
		switch (prefixSelected) {
		case 0:
			return ELVISH;

		case 1: 
			return ORCHIS;
			
		default:
			return null;
		}
	}
}
